/**
 * 
 */
package org.lora.biz.ou.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lora.biz.ou.LoraOUConstant;
import org.lora.biz.ou.entity.OrgLevel;
import org.lora.biz.ou.entity.OrgLevelExample;
import org.lora.biz.ou.entity.Organization;
import org.lora.biz.ou.entity.OrganizationExample;
import org.lora.biz.ou.exception.LoraOrgUserException;
import org.lora.biz.ou.mapper.OrgLevelMapper;
import org.lora.biz.ou.mapper.OrganizationMapper;
import org.lora.biz.ou.service.IOrganizationService;
import org.lora.core.annotation.AutoAssemble;
import org.lora.core.annotation.Service;
import org.lora.core.mybatis.OrderByBean;
import org.lora.core.mybatis.Page;
import org.lora.util.StringUtil;
import org.lora.webframework.common.FrameWorkBaseService;

/**
 * <p>
 * 类名:OrganizationServiceImpl
 * </p>
 * 描述:组织结构服务实现<br>
 * 创建时间:2016年8月31日 上午10:11:29<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年8月31日 Bladnir 创建<br>
 */
@Service
public class OrganizationServiceImpl extends FrameWorkBaseService implements IOrganizationService {

	@AutoAssemble
	private OrgLevelMapper orgLevelMapper;

	@AutoAssemble
	private OrganizationMapper organizationMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IOrganizationService#getOrgList(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public List<Organization> getOrgList(String domainId, String id, String parentId) throws LoraOrgUserException {

		OrganizationExample example = new OrganizationExample();

		org.lora.biz.ou.entity.OrganizationExample.Criteria criteria = example.createCriteria();

		if (StringUtil.isNotNull(domainId)) {
			criteria.andDomainIdEqualTo(domainId);
		}

		if (StringUtil.isNotNull(id)) {
			criteria.andIdEqualTo(id);
		}

		if (StringUtil.isNotNull(parentId)) {
			criteria.andParentIdEqualTo(parentId);
		}

		return organizationMapper.selectByExample(example);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IOrganizationService#getOrgLevelList(java.lang
	 * .String, java.util.Map, org.lora.core.mybatis.Page, java.util.List)
	 */
	@Override
	public List<OrgLevel> getOrgLevelList(String domainId, Map<String, Object> parameterMap, Page page, List<OrderByBean> orderByList)
			throws LoraOrgUserException {

		OrgLevelExample example = new OrgLevelExample();

		org.lora.biz.ou.entity.OrgLevelExample.Criteria criteria = example.createCriteria();
		setParameterForOrgLevel(domainId, parameterMap, criteria);

		// 排序
		String orderByClause = getOrderByClause(orderByList);
		if (StringUtil.isNotNull(orderByClause)) {
			example.setOrderByClause(orderByClause);
		}

		return orgLevelMapper.selectByExample(example, page);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IOrganizationService#getOrgLevelCount(java.lang
	 * .String, java.util.Map)
	 */
	@Override
	public int getOrgLevelCount(String domainId, Map<String, Object> parameterMap) throws LoraOrgUserException {

		OrgLevelExample example = new OrgLevelExample();

		org.lora.biz.ou.entity.OrgLevelExample.Criteria criteria = example.createCriteria();
		setParameterForOrgLevel(domainId, parameterMap, criteria);

		return orgLevelMapper.countByExample(example);
	}

	/**
	 * 设置查询参数
	 * 
	 * @param domainId
	 * @param parameterMap
	 * @param criteria
	 */
	private void setParameterForOrgLevel(String domainId, Map<String, Object> parameterMap,
			org.lora.biz.ou.entity.OrgLevelExample.Criteria criteria) {

		if (StringUtil.isNotNull(domainId)) {
			criteria.andDomainIdEqualTo(domainId);
		}

		String state = getStringFromMap("state", parameterMap);
		if (StringUtil.isNotNull(state)) {
			criteria.andStateEqualTo(state);
		}

		String orgLevelName = getStringFromMap("orgLevelName", parameterMap);
		if (StringUtil.isNotNull(orgLevelName)) {
			criteria.andOrgLevelNameLike(LoraOUConstant.PERCENT + orgLevelName + LoraOUConstant.PERCENT);
		}

		String orgLevel = getStringFromMap("orgLevel", parameterMap);
		if (StringUtil.isNotNull(orgLevel)) {
			criteria.andOrgLevelEqualTo(Integer.parseInt(orgLevel));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IOrganizationService#saveOrganization(org.lora
	 * .biz.ou.entity.Organization)
	 */
	@Override
	public int saveOrganization(Organization organization) throws LoraOrgUserException {

		String id = organization.getId();

		if (StringUtil.isNull(id)) {
			organization.setId(super.creatBeanId(LoraOUConstant.ORG_PREFIX));
			return organizationMapper.insert(organization);
		} else {
			OrganizationExample example = new OrganizationExample();
			example.createCriteria().andIdEqualTo(id);
			return organizationMapper.updateByExample(organization, example);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IOrganizationService#getAllParentOrganization
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public List<Organization> getAllParentOrganization(String domainId, String orgId) throws LoraOrgUserException {
		Map<String, Object> parameterMap = new HashMap<>();

		parameterMap.put("orgId", orgId);
		parameterMap.put("domainId", domainId);

		return organizationMapper.selectAllParent(parameterMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.ou.service.IOrganizationService#getAllChildrenOrganization
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public List<Organization> getAllChildrenOrganization(String domainId, String orgId) throws LoraOrgUserException {
		Map<String, Object> parameterMap = new HashMap<>();

		parameterMap.put("orgId", orgId);
		parameterMap.put("domainId", domainId);
		return organizationMapper.selectAllChildren(parameterMap);
	}

}
